﻿using System;
using System.Windows;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.UnityExtensions;
using Microsoft.Practices.Unity;
using Refract;
using Refract.Navigation;

namespace Refract.Demo
{
    /// <summary>
    /// Initializes the container and all dependencies for the application
    /// </summary>
    public class BootStrapper : UnityBootstrapper
    {
        #region Fields

        private INavigator _Navigator;

        private const string CatalogUrl = "Refract.Demo.Shell;component/ModulesCatalog.xaml";

        ShellViewModel _viewModel; 

        #endregion

        #region Overrides

        /// <summary>
        /// Creates the shell or main window of the application.
        /// </summary>
        /// <returns>The shell of the application.</returns>
        /// <remarks>
        /// If the returned instance is a <see cref="DependencyObject"/>, the
        /// <see cref="UnityBootstrapper"/> will attach the default <seealso cref="IRegionManager"/> of
        /// the application in its <see cref="RegionManager.RegionManagerProperty"/> attached property
        /// in order to be able to add regions by using the <seealso cref="RegionManager.RegionNameProperty"/>
        /// attached property from XAML.
        /// </remarks>
        protected override DependencyObject CreateShell()
        {
            
            _Navigator = Container.BuildUp(new Navigator("MainRegion"));
            Container.RegisterInstance<INavigator>(_Navigator, new ContainerControlledLifetimeManager());
            
            _viewModel = Container.BuildUp(new ShellViewModel());

            Shell shell = _viewModel.CreateView();

            Application.Current.RootVisual = shell;

            return shell;
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
        }

        /// <summary>
        /// Returns the module catalog that will be used to initialize the modules.
        /// </summary>
        /// <returns>
        /// An instance of <see cref="IModuleCatalog"/> that will be used to initialize the modules.
        /// </returns>
        /// <remarks>
        /// When using the default initialization behavior, this method must be overwritten by a derived class.
        /// </remarks>
        protected override IModuleCatalog GetModuleCatalog()
        {
            return ModuleCatalog.CreateFromXaml(new Uri(CatalogUrl, UriKind.Relative));
        } 

        #endregion
    }
}
